package cpcn.dsp.demo.tx.liveness.notify;

import cpcn.dsp.demo.consts.DSPConsts;
import cpcn.dsp.institution.api.notice.NoticeRequest;
import cpcn.dsp.institution.api.system.DSPInstitutionEnvironment;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @ClassName: ReceiveNoticePage
 * @Description: 接收页面通知
 * @Author: chengzi
 * @Date: 2020/7/2 0002 13:02
 * @Version: v1.0 文件初始创建
 */
public class ReceiveNoticePage extends HttpServlet {


    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //get方式不会
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) {
        String message = request.getParameter("Message");
        String signature = request.getParameter("Signature");
        String dgtlenvlp = request.getParameter("Dgtlenvlp");
        String signSN = request.getParameter("SignSN");
        String encryptSN = request.getParameter("EncryptSN");
        //解析
        //1.初始化
        try {
            DSPInstitutionEnvironment.initialize(DSPConsts.Keystore_test, DSPConsts.myKeystorePassword_test, DSPConsts.publicKey_test, DSPConsts.MSG_ENCRYPT_20);
            NoticeRequest noticeRequest = new NoticeRequest(message, signature, dgtlenvlp, signSN, encryptSN);
            String plainText = noticeRequest.getPlainText();
            String txCode = noticeRequest.getTxCode();
            String code = noticeRequest.getCode();
            String noticeMessage = noticeRequest.getMessage();
            System.out.println(noticeMessage);

            //////////////////////////////////
            //////解析明文后商户自己实现逻辑/////
            /////////////////////////////////
            //接收页面通知，响应的内容将会在识别结果页面展示
            PrintWriter out = response.getWriter();
            out.write(plainText);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
